import pageination from '../../components/pageination/pageination.vue'
export default {
	data() {
		return {
			//数据
      curriculumName: '',// 结业列表课程名字搜索

      report: '',
      modelStudentCurrContent: false,
			studentId: 0,
			studentName: '',
			cpage: 1,
			ctotal: 0,
			curr: [],

			modelStudentCurr: false, // 班级学员报告
			spage: 1,
			stotal: 0,

			startTime: '',
			endTime: '',

			curriculumSearch: '',
			currTotal: 0,
			currPage: 1,
			time: [],
			checkStudent: [], // 创建班级选中的学生
			allStudent: [], //所有学生
			NotSchedulingStudent: [], // 未排班学生
			className: '',
			classId: 0,
			classData: [], // 班级table
			teacherTableData: [], // 教师table
			currTableData: [], // 课程table
			studentData: [], // 班级下已有学生table
			teacherData: [], // 班级下已有教师table
			currData: [], // 班级下已有课程table
			curId: 0,
			modelShow: false, //创建班级
			modelShowStudents: false, //班级学员
			modelShowTeachers: false, //班级教师
			modelShowCurriculum: false, //班级课程
			total: 0, //总条数
			page: 1, //当前页
			size: 10, //每页显示条数
			classTotal: 0, // 班级总条数
			classPage: 1, //当前页
			classSize: 10, //每页显示条数
			chooseCourse:false,//是否显示选择课程弹框
			chooseCourseObj:{},//选中课程信息
		}
	},
	components: {
		pageination
	}, // year
	created() { //调用方法
		let that = this;

		// 所有学生学生
		that.$post(
				that.urls.findAllStudent, {
					"pageNum": 0,
					"pageSize": 7,
					auditStatus: 1
				} //    审核状态(0：待审核；1：审核通过；2：审核不通过)
			)
			.then(function(res) {
				if(res.success) {
					that.allStudent = res.pageInfo.list;
					that.total = res.pageInfo.total;
					that.page = res.pageInfo.pageNum;
					that.size = res.pageInfo.size;
				}
			})
	},
	mounted() {
    let da = new Date();
    let year =   da.getFullYear();
    this.time.push(year);
    this.time.push(year -1);
    this.time.push(year -2);
    this.time.push(year -3);
    this.time.push(year -4);


  },
	methods: { //写函数方法
		// 班级列表
    getData: function() {
      let that = this;
      let value = $('#searchSudentVal').val();
      that.$post(
        that.urls.classList, {
          "pageNum": that.classPage,
          "pageSize": that.classSize,
          "name": value,
          "year": $('#year').val()
        }
      )
        .then(function(res) {
          if(res.success) {
            that.classData = res.pageInfo.list;
            that.classTotal = res.pageInfo.total;
            that.classPage = res.pageInfo.pageNum;
          }
        })
    },

    //页码切换执行方法 结题报告
    cpageFn1: function(val, pid) {
      this.cpage = parseInt(val);
      //此处调列表数据接口方法
      this.viewCurr(); // 班级列表
    },
    /**返回子组件用户选择的一页显示条数**/
    cobtainChild1: function(data) {
      this.row = data;
    },

    // 结题报告
    viewCurr: function (studentId, classId, studentName) {
      let that = this;
     // that.studentName = '';
  //    that.studentId = 0;
    //  that.classId = 0;
      if(studentId > 0 ){
        that.cpage = 1;
        that.studentId = studentId;
      }
      if(classId  > 0){
        that.classId = classId;
      }
      if(studentName){
        that.studentName = studentName;
      }
        that.$post(
          that.urls.getStudentCurriculumPageInfo,
          {"studentId": that.studentId,"classId": that.classId, "pageNum": that.cpage, "pageSize": 10
          , "curriculumName": that.curriculumName

          }
        )
          .then(function(res) {
            if(res.success) {
              that.curr = res.pageInfo.list;
              that.cpage = res.pageInfo.pageNum;
              that.ctotal = res.pageInfo.total;
            }
          })

      this.modelStudentCurr = true;
    },

    //班级学生结业
    classStudentGraduation: function(studentId, classId) {
      let that = this;
      if (studentId != null && classId != null) {
        that.$post(
          that.urls.addGraduation, {
            "classId": classId,
            "studentId": studentId
          }
        )
          .then(function(res) {
            let msg = "";
            if(res.success) {
              msg = "结业成功！";
              //重新加载信息
              that.modelStudents(that.classId, that.className);
            } else {
              msg = "结业失败！";
            }
            layer.msg(res.message, {
              time: 3000, //3s后自动关闭
            });
          })
      }
    },
		// 学生列表选择框
		checkStu: function(id, name, identityCard) {
			let that = this;
			if($('#' + id).is(':checked') == false) {
				if(that.checkStudent.length > 0) {
					let checkArr = [];
					for(let i = 0, len = that.checkStudent.length; i < len; i++) { // 除去取消的数据
						let data = that.checkStudent[i];
						if(data.id == id) {
							continue;
						}
						checkArr.push(data);
					}
					that.checkStudent = checkArr;
				}
			} else {
				if(that.checkStudent.length > 0) {
					let checkList = [];
					let mark = 0; // 标识是否存在
					for(let i = 0, len = that.checkStudent.length; i < len; i++) { // 循环除去重复的数据
						let data = that.checkStudent[i];
						if(data.id == id) {
							mark = 1;
							continue;
						}
						checkList.push(data);
					}
					if(mark == 0) {
						checkList.push({
							id: id,
							name: name,
							identityCard: identityCard
						});
					} // 确定不存在保存
					that.checkStudent = checkList;
				} else {
					that.checkStudent.push({
						id: id,
						name: name,
						identityCard: identityCard
					}); // 没有数据直接加入
				}
			}
		},
		// 未排班学生选择
		checkStuThe: function(id, name, identityCard) {
			let that = this;
			// 增加选择的人数
			if(that.checkStudent.length > 0) {
				let checkList = [];
				for(let i = 0, len = that.checkStudent.length; i < len; i++) { // 循环除去重复的数据
					let data = that.checkStudent[i];
					if(data.id == id) {
						layer.alert("选择的学员已存在！");
						return;
					}
					checkList.push(data);
				}
				checkList.push({
					id: id,
					name: name,
					identityCard: identityCard
				}); // 确定不存在保存
				that.checkStudent = checkList;
			} else {
				that.checkStudent.push({
					id: id,
					name: name,
					identityCard: identityCard
				}); // 没有数据直接加入
			}

			if(that.NotSchedulingStudent.length > 0) {
				let checkList = [];
				for(let i = 0, len = that.NotSchedulingStudent.length; i < len; i++) { // 未排班的人员减一
					let data = that.NotSchedulingStudent[i];
					if(data.id == id) {
						continue;
					}
					checkList.push(data);
				}
				that.NotSchedulingStudent = checkList;
			}
		},
		// 未排班学生删除
		delStudent: function(id, name, identityCard) {
			let that = this;
			let checkList = [];
			for(let i = 0, len = that.checkStudent.length; i < len; i++) { // 循环减少删除的哪一条,选中的数据-1
				let data = that.checkStudent[i];
				if(data.id == id) {
					continue;
				}
				checkList.push(data);
			}
			that.checkStudent = checkList; // 赋值
			// 未排班的数据+1
			if(that.NotSchedulingStudent.length > 0) {
				let mark = 0; // 标志删除的是否在未排班里，有的话就不添加
				for(let i = 0, len = that.NotSchedulingStudent.length; i < len; i++) { // 未排班的人员减一
					let data = that.NotSchedulingStudent[i];
					if(data.id == id) {
						mark = 1;
						continue;
					}
				}
				if(mark == 0) {
					that.NotSchedulingStudent.push({
						id: id,
						name: name,
						identityCard: identityCard
					})
				}
			}
		},
		// 未排班学生分页
		getNotSchedulingStudent: function() {
			let that = this;
			that.$post(
					that.urls.getNotSchedulingStudent, {
						"pageNum": 0,
						"pageSize": 99999999,
						auditStatus: 1
					}
					//    审核状态(0：待审核；1：审核通过；2：审核不通过)
				)
				.then(function(res) {
					if(res.success) {
						that.NotSchedulingStudent = res.pageInfo.list;
						that.total = res.pageInfo.total;
						that.page = res.pageInfo.pageNum;
						that.size = res.pageInfo.size;
					}
				})
		},
		// 创建班级 和关联学生
		addClass: function() {
			let that = this;
			let name = $('#className').val();
			if(!name) {
				layer.alert("班级名称不能为空！");
				return;
			}
			that.$post(
					that.urls.addTraineeClass, {
						"name": name
					}
				)
				.then(function(res) {
					if(res.success) {
						let ids = res.data;
						let checkList = [];
						for(let i = 0, len = that.checkStudent.length; i < len; i++) {
							let data = that.checkStudent[i];
							checkList.push(data.id);
						}
						// 关联学生
						that.$post(
								that.urls.classAssStudent, {
									"classId": ids,
									"studentId": checkList
								}
							)
							.then(function(res) {
								if(res.success) {
									layer.alert(res.message);
									that.modelShow = false;
								}
							})

					}
				})
		},
		// 搜索学生列表
		searchSudent: function() {
			let that = this;

			// 所有学生学生
			that.$post(
					that.urls.findAllStudent, {
						"pageNum": 0,
						"pageSize": 7,
						auditStatus: 1
					}
				)
				.then(function(res) {
					if(res.success) {
						that.allStudent = res.pageInfo.list;
						that.total = res.pageInfo.total;
						that.page = res.pageInfo.pageNum;
					}
				})
		},
		//页码切换执行方法

    //页码切换执行方法
    spageFn1: function(val, pid) {
      this.spage = parseInt(val);
      //此处调列表数据接口方法
      this.modelStudents(); // 班级列表
    },
    /**返回子组件用户选择的一页显示条数**/
    sobtainChild1: function(data) {
      this.row = data;
    },


		pageFn: function(val, pid) {
			this.classPage = parseInt(val);
			this.getData();
			//此处调列表数据接口方法

		},
		/**返回子组件用户选择的一页显示条数**/
		obtainChild: function(data) {
			this.row = data;
		},
		//创建班级
		openModal: function() {
			let that = this;
			// that.checkStudent = []; // 默认选中的人数为0
			that.getNotSchedulingStudent(); // 查询未排班的学生
			that.modelShow = true;
		},
		//关闭模态框
		closeModel: function() {

			this.modelShow = false; //创建班级
      this.modelStudentCurr = false; //班级学员报告
			this.modelShowStudents = false; //班级学员
			this.modelShowTeachers = false; //班级教师
			this.modelShowCurriculum = false; //班级课程
		},

    closeReport : function () {
      this.modelStudentCurrContent = false;
    },

		//班级学员
		modelStudents: function(ids, name) {
			let that = this;
			that.modelShowStudents = true;
			if(name){
        that.className = name;
      }
			if(ids){
        that.classId = ids;
      }

			// 班级列表
			that.$post(
					that.urls.findStudentByClassId, {
          id: that.classId,"pageNum": that.spage,"pageSize": 10
					}
				)
				.then(function(res) {
					if(res.success) {
						that.studentData = res.pageInfo.list;
            that.spage = res.pageInfo.pageNum;
            that.stotal = res.pageInfo.total;
					}
				})
		},
		//班级教师
		modelTeachers: function(ids, name) {
			let that = this;
			that.modelShowTeachers = true;
			that.className = name;
			that.classId = ids;
			// teacherTableData
			// 全部教师
			that.$post(
					that.urls.getTeacherPaging, {
						"pageNum": 0,
						"pageSize": 9999999
					}
				)
				.then(function(res) {
					if(res.success) {
						that.teacherTableData = res.pageInfo.list;
					}
				})
			// 选中的教师
			that.$post(
					that.urls.findTeacherClassId, {
						id: ids
					}
				)
				.then(function(res) {
					if(res.success) {
						that.teacherData = res.data;
					}
				})
		},
    //页码切换执行方法
    pageFncurr: function(val, pid) {
      this.currPage = parseInt(val);
      //此处调列表数据接口方法
      this.modelCurriculum(); // 班级列表
    },
    /**返回子组件用户选择的一页显示条数**/
    obtainChildcurr: function(data) {
      this.row = data;
    },

		//班级课程
		modelCurriculum: function(ids, name) {
			let that = this;
			that.chooseCourseObj = {};
			that.currTableData = [];
			that.modelShowCurriculum = true;

			if(ids > 0){
        that.currData = [];
        that.curriculumSearch = '';
        that.classId = ids;
      }
			// 全部课程
      that.$post(
        that.urls.getCurriculumPageInfo,
        {"search": that.curriculumSearch,
          "pageNum": that.currPage,
          "pageSize": 10,
        }
      )
        .then(function(res) {
          if(res.success) {
            that.currTableData = res.pageInfo.list;
            that.currTotal = res.pageInfo.total;
            that.currPage = res.pageInfo.pageNum;
          }
        })

      if(name){
        that.className = name;
      }
      if(ids > 0){
        that.$post(
          that.urls.findCurriculumByClassId, {
            id: ids,
            "pageNum": 1,
            "pageSize": 10000,
          }
        )
          .then(function(res) {
            if(res.success) {
              that.currData = res.pageInfo.list;
            }
          })
      }

		},
		// 删除班级下的学生
		delExisStudent: function(id) {
			let that = this;
			let checkList = [];
			for(let i = 0, len = that.studentData.length; i < len; i++) {
				let data = that.studentData[i];
				if(id == data.id) {
					continue;
				}
				checkList.push(data);
			}
			that.studentData = checkList;
		},
		// 修改班级的学生 确定
		subEditStudent: function() {
			let that = this;
			let checkList = [];
			let classId = that.classId;
			if(that.studentData.length <= 0) {
				layer.msg('选择的学员不能为空!', {
					time: 3000, //3s后自动关闭
				});
				return;
			}
			for(let i = 0, len = that.studentData.length; i < len; i++) {
				let data = that.studentData[i];
				checkList.push(data.id);
			}
			// 提交
			that.$post(
					that.urls.ClassEditStudent, {
						"classId": classId,
						"studentId": checkList
					}
				)
				.then(function(res) {
					if(res.success) {
						that.modelShowStudents = false;
						layer.msg(res.message, {
							time: 3000, //3s后自动关闭
						});
					}
				})
		},
		// 班级修改教师
		subClassTeacher: function() {
			let that = this;
			let checkList = [];
			let classId = that.classId;
			if(that.currData.length <= 0) {
				layer.msg('选择的教师不能为空!', {
					time: 3000, //3s后自动关闭
				});
				return;
			}
			for(let i = 0, len = that.currData.length; i < len; i++) {
				let data = that.currData[i];
				checkList.push(data.id);
			}
			// 提交
			that.$post(
					that.urls.ClassEditTeacher, {
						"classId": classId,
						"teacherId": checkList
					}
				)
				.then(function(res) {
					if(res.success) {
						that.modelShowTeachers = false;
						layer.msg(res.message, {
							time: 3000, //3s后自动关闭
						});
					}
				})
		},
		// 班级修改课程
		subClassCurr: function() {
			let that = this;
			let checkList = that.currData;
	/*		checkList.forEach(function (e) {
        e.classId = e.classid;
        e.curriculumId = e.curriculumid;
        e.biginTime = e.bigintime;
        e.endTime = e.endtime;
        e.createAt = e.create_at;
      })*/

      console.log(checkList)
			// 提交
      if(checkList == null || checkList.length <= 0){
        that.$post(
          that.urls.ClassDelCurriculum, {
            "id": that.classId
          }
        )
          .then(function(res) {
            if(res.success) {
              that.modelShowCurriculum = false;
              layer.msg(res.message, {
                time: 3000, //3s后自动关闭
              });
            }
          })
      }else {
        that.$post(
          that.urls.ClassEditCurriculum, {
            "list": checkList
          }
        )
          .then(function(res) {
            if(res.success) {
              that.modelShowCurriculum = false;
              layer.msg(res.message, {
                time: 3000, //3s后自动关闭
              });
            }
          })
      }

		},
		// 班级选择教师
		chooseTeacher: function(id, name, staffNumber) {
			let that = this;
			// 增加选择的人数
			if(that.teacherData.length > 0) {
				let checkList = [];
				for(let i = 0, len = that.teacherData.length; i < len; i++) { // 循环除去重复的数据
					let data = that.teacherData[i];
					if(data.id == id) {
						layer.alert("选择的教师已存在！");
						return;
					}
					checkList.push(data);
				}
				checkList.push({
					id: id,
					name: name,
					staffNumber: staffNumber
				}); // 确定不存在保存
				that.teacherData = checkList;
			} else {
				that.teacherData.push({
					id: id,
					name: name,
					staffNumber: staffNumber
				}); // 没有数据直接加入
			}

			if(that.teacherTableData.length > 0) {
				let checkList = [];
				for(let i = 0, len = that.teacherTableData.length; i < len; i++) { // 教师人员减一
					let data = that.teacherTableData[i];
					if(data.id == id) {
						continue;
					}
					checkList.push(data);
				}
				that.teacherTableData = checkList;
			}
		},
		// 删除已选择的教师
		delTeacher: function(id, name, staffNumber) {
			let that = this;

			let checkList = [];
			for(let i = 0, len = that.teacherData.length; i < len; i++) { // 循环减少删除的哪一条,选中的数据-1
				let data = that.teacherData[i];
				if(data.id == id) {
					continue;
				}
				checkList.push(data);
			}
			that.teacherData = checkList; // 赋值
			// 未排班的数据+1
			if(that.teacherTableData.length > 0) {
				let mark = 0; // 标志删除的是否在教师，有的话就不添加
				for(let i = 0, len = that.teacherTableData.length; i < len; i++) { // 教师人员减一
					let data = that.teacherTableData[i];
					if(data.id == id) {
						mark = 1;
						continue;
					}
				}
				if(mark == 0) {
					that.teacherTableData.push({
						id: id,
						name: name,
						staffNumber: staffNumber
					})
				}
			}
		},
		//关闭选择课程弹框
		closeCourseModel:function(){
			this.chooseCourse=false;
		},
		//班级确定选择
		sureChoose:function(){
			let  that = this;

			if(that.chooseCourseObj.endTime < that.chooseCourseObj.biginTime){
        layer.msg('结课时间不能小于开课时间!', {
          time: 3000, //3s后自动关闭
        });
			  return;
      }

			if(!that.chooseCourseObj.addr || that.chooseCourseObj.addr === undefined || that.chooseCourseObj.addr == null){
        layer.msg('上课地点不能为空!', {
          time: 3000, //3s后自动关闭
        });
        return;
      }

      let create = new Date();  // 创建时间
      that.chooseCourseObj.createAt = create;


      console.log(formatData(that.chooseCourseObj.biginTime)  )
      console.log(formatData(that.chooseCourseObj.endTime)  )
       console.log(that.chooseCourseObj  )

			that.currData.push(that.chooseCourseObj);

			if(that.currTableData.length > 0) {
				let checkList = [];
				for(let i = 0, len = that.currTableData.length; i < len; i++) { // 课程减一
					let data = that.currTableData[i];
					if(data.id == that.chooseCourseObj.id) {
						continue;
					}
					checkList.push(data);
				}
				that.currTableData = checkList;
			}
			that.chooseCourseObj = {};
      that.chooseCourse = false;
		},
		// 班级下选择课程
		chooseCurr: function(id, name) {
			let that = this;


			if(that.currData.length > 0) {
				let checkList = [];
				for(let i = 0, len = that.currData.length; i < len; i++) { // 循环除去重复的数据
					let data = that.currData[i];
					if(data.curriculumId == id) {
						layer.alert("选择的课程已存在！");
						return;
					}
					checkList.push(data);
				}
				/*checkList.push({
					id: id,
					name: name,
          createAt : create,
				}); // 确定不存在保存
				that.currData = checkList;*/
			/*} else {
				that.currData.push({
					id: id,
					name: name ,
          createAt : create,
				}); // 没有数据直接加入*/
			}
			this.chooseCourseObj.name = name;
			this.chooseCourseObj.id = id;
			that.chooseCourseObj.curriculumId = id;
      this.chooseCourseObj.classId = that.classId;

			this.chooseCourse = true;
			/*if(that.currTableData.length > 0) {
				let checkList = [];
				for(let i = 0, len = that.currTableData.length; i < len; i++) { // 课程减一
					let data = that.currTableData[i];
					if(data.id == id) {
						continue;
					}
					checkList.push(data);
				}
				that.currTableData = checkList;
			}*/
		},
		// 删除班级下的课程
		delChooseCurr: function(id, name) {
			let that = this;
      console.log(id)
      console.log(name)
			let checkList = [];
			for(let i = 0, len = that.currData.length; i < len; i++) { // 循环减少删除的哪一条,选中的数据-1
				let data = that.currData[i];
				if(data.curriculumId == id) {
					continue;
				}
				checkList.push(data);
			}
			that.currData = checkList; // 赋值
			// 未排班的数据+1
			if(that.currTableData.length > 0) {
				let mark = 0; // 标志删除的是否在课程，有的话就不添加
				for(let i = 0, len = that.currTableData.length; i < len; i++) { // 课程减一
					let data = that.currTableData[i];
					if(data.id == id) {
						mark = 1;
						continue;
					}
				}
				if(mark == 0) {
					that.currTableData.push({
						id: id,
						name: name
					})
				}
			}else {
        that.currTableData.push({
          id: id,
          name: name
        })
      }
		},





		toDateStr(timestamp) {
			let time = new Date(timestamp) // 先将时间戳转为Date对象，然后才能使用Date的方法
			let year = time.getFullYear(),
				month = time.getMonth() + 1, // 月份是从0开始的
				day = time.getDate();
			//  hour = time.getHours(),
			//  minute = time.getMinutes(),
			//  second = time.getSeconds();
			//  return year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second;
			return year + '-' + month + '-' + day;
		},
    closethe: function () {
      this.modelStudentCurr = false;
    },

    // 跳转到预览
    toSrc: function (url) {
      if(url == null || url == undefined || url == ''){
        layer.msg('暂无附件', {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      window.open(this.urls.baseUrl + url);
    },

    viewReport: function (e) {
      this.report = e;
      this.modelStudentCurrContent = true;
    }

	}
}
